
/*
	(c) Galustyan Sergey 2009
*/

#include "dxut.h"
#include "Main.h"
#include "Land.h"
#include "Spheres.h"
#include "RayComputer.h"
#include "OclQueryComputer.h"

namespace TestTask
{
	Renders			renders;

	PRayComputer		computer;
	POclQueryComputer	oclComputer;

	hres Init( PDevice device ){
		Destroy();

		PLandscapeRender lr( new LandscapeRender( device));
		renders.push_back( lr );

		PSuperSphere ss1( new SuperSphere( device, 20, Vector3( 50, 50, 70 ), Color(1,0,0,1), lr));
		PSuperSphere ss2( new SuperSphere( device, 20, Vector3( -80, -100, 80 ), Color(0,1,0,1), lr));

		renders.push_back( PRenderObject( new SphereRender(device, 10, Vector3(0,0,0), Color(1,1,1,1))));
		
		renders.push_back( ss1 );
		renders.push_back( ss2 );

		renders.push_back( PRenderObject( new LineRender( device, ss1, ss2)));

		computer.reset( new RayComputer( device, ss1, ss2, lr->GetHeightTexture()));
		oclComputer.reset( new OclQueryComputer( device, ss1, ss2, lr ) );

		return S_OK;
	}

	hres PreRender( RenderPackage& rp ){
		static float timeout = 0;
		timeout -= rp.elapsed;
		
		if ( timeout > 0 ) return S_OK;
		else timeout = 0.3f;

		computer->specialRender( rp );
		oclComputer->specialRender( rp );

		return S_OK;
	}

	hres Render( RenderPackage& rp ){		
		rp.viewProj = rp.view*rp.proj;
		foreach(PRenderObject obj, renders) obj->RenderMain(rp);
		return S_OK;
	}

	hres RenderText( Text& text ){
		computer->getStat( text );
		oclComputer->getStat( text );
		return S_OK;
	}

	hres DeviceReset( PDevice ){
		oclComputer->devicereset();
		return S_OK;
	}

	hres DeviceLost(){
		oclComputer->devicelost();
		return S_OK;
	}
		
	void Destroy(){
		oclComputer.reset();
		computer.reset();
		renders.clear();
	}
}